#!/bin/bash

# Slackware build script for calf

# Written by B. Watson (urchlay@slackware.uk)

# 20211204 bkw: BUILD=3, move html docs to subdir.
# 20200117 bkw: demote lash from required to optional dependency.
# 20191208 bkw: update for v0.90.3.

# 20180709 bkw:
# - Update to latest release (less than a day old).
# - Use correct upstream homepage.

# 20170622 bkw:
# - Update to latest git. It's been a long time since the last release.
# - Actually make the SSE=yes option work (d'oh!)

# 20170301 bkw: use long-format github URL, no more $VERSION.tar.gz

# 20151106 bkw:
# Switch to -master and upgrade to v0.0.60. No more LADSPA or DSSI
# support (upstream dropped it). But if you need LADSPA, there's a
# separate calf-ladspa build now. If there's a popular demand for
# DSSI, I'll add it to calf-ladspa, not here.
# fluidsynth is now required, because the build fails without it, even
# though it's listed as experimental and there's a --disable-experimental
# option. Since it's required anyway, might as well --enable-experimental.

# 20141030 bkw:
# Finally getting around to submitting this, there have been no code
# changes upstream since 20140308.

# 20140308 bkw:
# - Switched to falkTX's KXStudio fork of CALF. falkTX doesn't do release
#   tarballs, so this is a git checkout from 20140308.
# - Added capability stuff.

cd $(dirname $0) ; CWD=$(pwd)

PRGNAM=calf
VERSION=${VERSION:-0.90.3}
BUILD=${BUILD:-3}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE"
  exit 0
fi

TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

if [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -march=i586 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION
tar xvf $CWD/$PRGNAM-$VERSION.tar.?z
cd $PRGNAM-$VERSION
chown -R root:root .
find -L .  -perm /111 -a \! -perm 755 -a -exec chmod 755 {} \+ -o \
        \! -perm /111 -a \! -perm 644 -a -exec chmod 644 {} \+

autoreconf -if

# Note: The build ignores the provided -O2 in the flags (but the
# -march/-mtune -fPIC stuff is used). If you really want to force
# it to use -O2 (Slackware default), set FORCE_SLACK_CFLAGS=yes
# in the environment.
if [ "${FORCE_SLACK_CFLAGS:-no}" = "yes" ]; then
  sed -i -e 's/ -O3[^"]*//' configure
  SSE=no
fi

# Build with SSE support?
case "${SSE:-auto}" in
  "yes") SSEOPT="--enable-sse"  ;;
  "no")  SSEOPT="--disable-sse" ;;
  *)     grep sse /proc/cpuinfo >/dev/null \
              && SSEOPT="--enable-sse" \
              || SSEOPT="--disable-sse" ;;
esac

echo "=== SSEOPT: $SSEOPT"

# For the slack-desc:
WITHSSE="without"
[ "$SSEOPT" = "--enable-sse" ] && WITHSSE="with"

# 20211204 bkw: --docdir and --htmldir accepted and ignored.
# htmldir on the make command line is also ignored (html docs go to docdir).
CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure \
  $SSEOPT \
  --enable-experimental \
  --prefix=/usr \
  --libdir=/usr/lib${LIBDIRSUFFIX} \
  --sysconfdir=/etc \
  --localstatedir=/var \
  --mandir=/usr/man \
  --enable-shared \
  --disable-static \
  --with-lv2-dir=/usr/lib${LIBDIRSUFFIX}/lv2 \
  --build=$ARCH-slackware-linux

make
make install \
     DESTDIR=$PKG \
     docdir=/usr/doc/$PRGNAM-$VERSION/html \

# install-strip is supported, but doesn't work:
strip $PKG/usr/bin/* $PKG/usr/lib*/{lv2/calf.lv2/calflv2gui.so,calf/calf.so}

gzip $PKG/usr/man/man?/*

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a AUTHORS COPYING* ChangeLog README TODO $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

# This shouldn't be in the package:
rm -f $PKG/usr/share/icons/hicolor/icon-theme.cache

# Shipped .desktop file doesn't validate, use modified copy:
cat $CWD/$PRGNAM.desktop > $PKG/usr/share/applications/$PRGNAM.desktop

mkdir -p $PKG/install
sed "s,@WITHSSE@,$WITHSSE," $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/doinst.sh > $PKG/install/doinst.sh

# Only add capability stuff if not disabled:
if [ "${SETCAP:-yes}" = "yes" ]; then
  cat $CWD/setcap.sh >> $PKG/install/doinst.sh
  # Only allow execution by audio group
  chown root:audio $PKG/usr/bin/$PRGNAM*
  chmod 0750 $PKG/usr/bin/$PRGNAM*
fi

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE
